
#include <stdio.h>
#include <stdlib.h>

#include <QThread>
#include <QtGui>
#include <QDebug>

#include "include/mainwindow.h"

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

MainWindow::MainWindow(QWidget *parent)
        : QWidget(parent) {
    // create background image
    m_canvasW = 900;
    m_canvasH = 900;
    m_img = new QImage(m_canvasW, m_canvasH, QImage::Format_RGB888);
    m_img->fill(QColor(0xff, 0xff, 0xff));

    // set window properties
    setBackgroundRole(QPalette::Base);
    setAutoFillBackground(true);

    setGeometry(20, 35, m_canvasW + 20, m_canvasH + 20);

    // set focus to self
    setFocus();
}


void MainWindow::timerEvent(QTimerEvent *event) {

}

//用于初始化文件到QT的地图，数据同步
void MainWindow::initMap(int8_t **rMap, int m_line, int m_row) {

    enum RMapItemType {
        RMAP_FREE = 0,                    // 空白区域 白色
        RMAP_OBSTACLE_UNDESCOVERED = 1,        // 未被发现的障碍物 深灰色
        RMAP_OBSTACLE_DILATE = 2,        // 膨胀的障碍物 浅灰色
        RMAP_OBSTACLE,                    // 障碍物
        RMAP_OBSTACLE_SCANNED,            // 已被扫描到的障碍物 黑色
        RMAP_START_POINT,                // 起点
        RMAP_END_POINT,                    // 终点
        RMAP_CURRENT_POSITION,            // 当前点
        RMAP_OBSTACLE_EDGE                // 障碍物扩展的点
    };

    Qt::GlobalColor mapping[10];
    mapping[RMAP_FREE] = Qt::white;
    mapping[RMAP_OBSTACLE_UNDESCOVERED] = Qt::darkGray;
    mapping[RMAP_OBSTACLE_DILATE] = Qt::lightGray;
    mapping[RMAP_OBSTACLE_SCANNED] = Qt::black;

    for (int32_t i = 0; i < m_line; i++) {
        for (int32_t j = 0; j < m_row; j++) {
            drawPoint(i, j, mapping[rMap[i][j]]);
        }
    }
}

void MainWindow::drawPoint(int j, int i, Qt::GlobalColor gc) {
    QPainter painter;

    int offset = 10;
    int grid_size = 4;

    painter.begin(m_img);

    painter.setRenderHint(QPainter::Antialiasing, true);

    painter.setBrush(gc);

    painter.drawRect(offset + i * grid_size, offset + j * grid_size, grid_size, grid_size);

    painter.end();

    this->update();

}

void MainWindow::clearPoint(int j, int i) {
    drawPoint(j, i, Qt::white);
}


void MainWindow::drawGrid() {
    QPainter painter;

    // 'o' - draw grid
    if (true) {
        // begin draw to background image
        painter.begin(m_img);
        painter.setRenderHint(QPainter::Antialiasing, true);

        int offset;
        int grid_size, nx, ny;
        double x1, y1, x2, y2;

        offset = 10;
        grid_size = 4;
        nx = 132;
        ny = 200;
        painter.drawLine(100, 10, 100, 710);
        // draw vertical lines
        y1 = offset + 0;
        y2 = offset + ny * grid_size;
        for (int i = 0; i <= nx; i++) {
            x1 = offset + i * grid_size;
            painter.drawLine(x1, y1, x1, y2);

        }

        // draw horiztonal lines
        x1 = offset + 0;
        x2 = offset + nx * grid_size;
        for (int i = 0; i <= ny; i++) {
            y1 = offset + i * grid_size;
            painter.drawLine(x1, y1, x2, y1);
        }

        painter.end();
    }

    this->update();
}

void MainWindow::keyPressEvent(QKeyEvent *event) {

     //'c' - clean canvas


    // 'q' - quit the program
    if (event->key() == Qt::Key_Q) {
        this->close();
    }

    this->update();
}

void MainWindow::mousePressEvent(QMouseEvent *event) {
}

void MainWindow::mouseMoveEvent(QMouseEvent *event) {
}

void MainWindow::paintEvent(QPaintEvent * /* event */) {
    QPainter painter(this);

    // draw offline image to screen
    painter.drawImage(QPoint(0, 0), *m_img);
}

